package net.entelijan.cobean.data.literature.persistence;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

public class ClauseBuilderTestCase {

	@Test
	public void testOrderByClauseIterating00() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new IterableClauseMapping<Object>(null) {

					@Override
					protected Object map(Object elem) {
						throw new IllegalStateException(
								"Should never be called");
					}

				});
		Assert.assertEquals("", cb.buildClause());
	}

	@Test
	public void testOrderByClauseIterating01() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new IterableClauseMapping<Object>(Arrays
						.asList(new Object[] {})) {

					@Override
					protected Object map(Object elem) {
						throw new IllegalStateException(
								"Should never be called");
					}

				});
		Assert.assertEquals("", cb.buildClause());
	}

	@Test
	public void testOrderByClauseIterating02() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new IterableClauseMapping<String>(Arrays.asList(new String[] {
						"A", "B" })) {

					@Override
					protected Object map(String elem) {
						return mapAB(elem);
					}

				});
		Assert.assertEquals("order by a, b", cb.buildClause());
	}

	@Test
	public void testOrderByClauseIterating03() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new IterableClauseMapping<String>(Arrays.asList(new String[] {
						"B", "A" })) {

					@Override
					protected Object map(String elem) {
						return mapAB(elem);
					}

				});
		Assert.assertEquals("order by b, a", cb.buildClause());
	}

	@Test
	public void testOrderByClauseIterating04() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new IterableClauseMapping<String>(Arrays.asList(new String[] {
						"B", "A", "C" })) {

					@Override
					protected Object map(String elem) {
						return mapAB(elem);
					}

				});
		Assert.assertEquals("order by b, a", cb.buildClause());
	}

	@Test
	public void testOrderByClauseIterating05() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new IterableClauseMapping<String>(Arrays.asList(new String[] {
						null, "B", "A", "C", null })) {

					@Override
					protected Object map(String elem) {
						return mapAB(elem);
					}

				});
		Assert.assertEquals("order by b, a", cb.buildClause());
	}

	@Test
	public void testOrderByClause01() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new ClauseMapping() {

					@Override
					public List<?> getClauseItems() {
						ArrayList<String> re = new ArrayList<String>();
						re.add("a.hallo");
						re.add("b.go");
						return re;
					}
				});
		Assert.assertEquals("order by a.hallo, b.go", cb.buildClause());
	}

	@Test
	public void testOrderByClause02() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new ClauseMapping() {

					@Override
					public List<?> getClauseItems() {
						ArrayList<String> re = new ArrayList<String>();
						re.add("a.hallo");
						return re;
					}
				});
		Assert.assertEquals("order by a.hallo", cb.buildClause());
	}

	@Test
	public void testEmptyOrderByClause() {
		ClauseBuilder cb = new ClauseBuilder("order by", ",",
				new ClauseMapping() {

					@Override
					public List<?> getClauseItems() {
						ArrayList<String> re = new ArrayList<String>();
						return re;
					}
				});
		Assert.assertEquals("", cb.buildClause());
	}

	private Object mapAB(String elem) {
		Object re = null;
		if ("A".equals(elem)) {
			re = "a";
		} else if ("B".equals(elem)) {
			re = "b";
		}
		return re;
	}

}
